// pages/meeting/add.js
import {post} from '../../utils/http'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    actionShow: false,
    actions:[],
    detail: {},
    formData:{},
    submiting: false,
    formList:[
      {
        name:"company",
        title: "参会单位",
        value: "",
        type:"text",
        input: "input",
        error: false,
        placeholder: "请输入参会单位",
        validate:(data)=>{
          if(data){
            return true
          }
          return false
        }
      },
      {
        name:"gender",
        title: "性别",
        value: "",
        type:"text",
        input: "select",
        array:[
          {
            name:"男",
            value: "1"
          },
          {
            name:"女",
            value: "0"
          }
        ],
        error: false,
        placeholder: "请选择性别",
        validate:(data)=>{
          return true
        }
      },{
        name:"phone",
        title: "手机号",
        value: "",
        type:"number",
        input: "input",
        error: false,
        placeholder: "请输入参会手机号",
        validate:(data)=>{

          return /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(data)
        }
      },{
        name:"email",
        title: "邮箱",
        value: "",
        type:"text",
        input: "input",
        error: false,
        placeholder: "请输入参会邮箱",
        validate:(data)=>{
          if(!data){
            return true
          }
          return /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(data)
        }
      },{
        name:"arrivalMethod",
        title: "到达方式",
        value: "",
        type:"text",
        input: "select",
        array:[
          {
            name:"飞机",
            value: "飞机"
          },
          {
            name:"火车",
            value: "火车"
          },
          {
            name:"汽车",
            value: "汽车"
          },
          {
            name:"公共交通",
            value: "公共交通"
          }],
        error: false,
        placeholder: "请选择到达方式",
        validate:(data)=>{
          return true
        }
      },{
        name:"arrivalVehicle",
        title: "车次/航班",
        value: "",
        type:"text",
        input: "input",
        error: false,
        placeholder: "请输入车次/航班",
        validate:(data)=>{
          return true
        }
      },{
        name:"arrivalTime",
        title: "参会日期",
        value: "",
        type:"text",
        input: "date",
        error: false,
        placeholder: "请选择参会日期",
        validate:(data)=>{
          return true
        }
      }
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const event =  this.getOpenerEventChannel()
    const that = this
    event.on("meetingData" , function(data) {
      that.setData({detail:data})
      that.submit = false
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  onCheck(e){
    const index = e.currentTarget.dataset.idx
    const data = e.detail.detail.value
    this.checkValidate(index,data)
  },

  onInput(e){
    const index = e.currentTarget.dataset.idx
    const data = e.detail.detail.value

    if(!this.checkValidate(index , data)){
      return;
    }

    let fromList = this.data.formList
    fromList[index].value = data
    fromList[index].error = false
    
    let formData = this.data.formData
    formData[fromList[index].name] = data

    this.setData({
      formList:fromList,
      formData: formData
    })
  },

  checkValidate(index,value){
    let fromList = this.data.formList

    if(!fromList[index].validate(value)){
      let msg = `${fromList[index].title}不能为空`
      if(value){
        msg = `${fromList[index].title}的值不正确`
      }
      wx.showToast({
        title: msg,
        icon: 'none'
      })
      fromList[index].error = true
      fromList[index].value = value
      this.setData({
        formList:fromList
      })
      return false;
    }
    return true;
  },

  onDateChange(e){
    const index = e.currentTarget.dataset.idx
    if(!this.checkValidate(index , e.detail.value)){
      return;
    }

    let fromList = this.data.formList
    fromList[index].value = e.detail.value
    fromList[index].error = false
    
    let formData = this.data.formData
    formData[fromList[index].name] = e.detail.value

    this.setData({
      formList:fromList,
      formData: formData
    })
  },

  onSelect(e){
    const index = e.detail.index
    let data = this.data.actions[index]

    if(!this.checkValidate(data.index,data.value)){
      return;
    }

    let fromList = this.data.formList
    fromList[data.index].value = data.name
    fromList[data.index].error = false

    let formData = this.data.formData
    formData[fromList[data.index].name] = data.value

    this.setData({
      actionShow: false,
      formData: formData,
      formList: fromList,
      actions:[]
    })
  },

  onOpenSelect(e){
    const index = e.currentTarget.dataset.idx
    const arr = this.data.formList[index].array
    arr.forEach(a=>{
      a['index'] = index
    })
    this.setData({
      actionShow: true,
      actions:arr
    })
  },

  onCloseSelect(){
    this.setData({
      actionShow: false,
      actions:[]
    })
  },

  handleSubmit(){
    if(!this.checkAllData()){
      return
    }
    if(this.submit){
      return
    }
    this.submit = true
    this.setData({
      submiting: true
    })
    const formData = this.data.formData
    formData['meetingId'] = this.data.detail.id
    const that = this
    post('/user/receipt/write' ,formData )
    .then(res=>{
      wx.showToast({
        title: '回执成功',
        icon: 'success',
        color: '#19be6b'
      })
      that.resetData()

      const pages = getCurrentPages()
      const prePage = pages[pages.length - 2]
      wx.navigateBack({
        success:function (params) {
          wx.setStorageSync(`${that.data.detail.id}`, "true")
          prePage.setAddSuccess({id: that.data.detail.id , success:true})
        }
      })
    }).catch(err=>{
      console.log(err.message)
      if(err.message.includes('您已填写过')){
        that.resetData()
        const pages = getCurrentPages()
        const prePage = pages[pages.length - 2]
        wx.navigateBack({
          success:function (params) {
            wx.setStorageSync(`${that.data.detail.id}`, "true")
            prePage.setAddSuccess({id: that.data.detail.id , success:true})
          }
        })
        return
      }
      wx.showToast({
        title: '回执失败,请稍后重试',
        icon: 'none',
      })
      that.setData({
        submiting: false,
      })
      that.submit = false
    })
  },
  resetData(){
    const formList = this.data.formList
    formList.forEach(it=>{
      it.value = ""
      it.error = false
    })
    this.setData({
      formData: {},
      formList: formList,
      submiting: false
    })
    this.submit = false
  },

  checkAllData(){
    const list = this.data.formList
    for(let i=0;i<list.length;i++){
      const item = list[i]
      if(!this.checkValidate(i,item.value)){
        return false
      }
    }
    return true
  }
})